package com.tomdxz.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tomdxz.dao.LoginDao;
import com.tomdxz.model.User;
import com.tomdxz.util.JDBCutil;

public class LoginDaoImpl implements LoginDao{

	@Override
	public User getUserByUaccountAndUpsw(String uAccount, String uPsw) {
		User u = new User();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT uid,uaccount,upsw,uname,rid FROM c_user");
		sql.append(" WHERE uaccount = ? AND upsw = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, uAccount);
			pst.setString(2, uPsw);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				u.setuId(rs.getInt("uid"));
				u.setuAccount(rs.getString("uaccount"));
				u.setuPsw(rs.getString("upsw"));
				u.setuName(rs.getString("uname"));
				u.setRid(rs.getInt("rid"));
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return u;
	}

}
